package com.freework.sys.permissions.system.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.freework.freedbm.exception.DAOException;
import com.freework.sys.permissions.external.service.SessionInfo;
import com.freework.sys.permissions.system.dao.FuncDAO;
import com.freework.sys.permissions.system.vo.SysFuncVO;
import com.freework.sys.permissions.system.vo.UserVO;
import com.freework.tree.TreeStructureServiceI;
import com.freework.util.FormatUtil;
@Service("freework.sys.permissions.system.service.FuncEditServiceImpi")
public class FuncEditServiceImpi implements FuncEditServiceI {
	@Resource
	FuncDAO dao=null;
	
	
	
	public void setDao(FuncDAO dao) {
		this.dao = dao;
	}
	public void addFunc(SysFuncVO vo) throws DAOException {
		vo.setCreatetime(FormatUtil.stringDateTime2());
		Integer Userid=SessionInfo.getUser().getUserid();
		vo.setCreateuser(Userid);
		dao.insertPermission(vo, "funccode");
	}

	public void deleteFunc(Integer funcid) throws DAOException {
		dao.delete(funcid);

	}

	public List queryTree(SysFuncVO vo,UserVO u) {
		String uFuncStr="";
		if(vo.getPfuncid()==null)
			vo.setPfuncid(0);
		if(u!=null)
		{
			if(u.getUsertype()!=1)
			{
				uFuncStr+=" and funcid in (SELECT funcid from sys_groupfunc t left join sys_groupuser u on u.groupid=t.groupid where u.userid="+u.getUserid()+")";
			}
		}
		List<SysFuncVO> list=dao.query(vo,"  1=1 "+uFuncStr+" order by menuno");
		
		for (SysFuncVO sysFuncVO : list) {
			int count=dao.getJdbcTemplate().queryForInt("select count(*) from sys_func where pfuncid="+sysFuncVO.getFuncid());
			sysFuncVO.setLeaf(count==0);
		}
		
		return  list;
		

	}

	public void updateFunc(SysFuncVO vo) throws DAOException {
		dao.updatePermission(vo, "funccode");

	}

}
